package vlt.el.db;

import java.io.FileNotFoundException;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import java.sql.DriverManager;

import vlt.el.util.Config;

public class DBConnection {
    
    public Connection connOrig = null;
    public Connection connDest = null;
    //private static DBConnection instance;
    
    public DBConnection(){    	
    	
    }
    
//    public static DBConnection getInstance(){
//    	if (instance==null){
//    		instance = new DBConnection();
//    	}
//    	return instance;
//    }
	
    public Connection getConnOrig(){
    	return connOrig;
    }
    
    public Connection getConnDest(){
    	return connDest;
    }
    
	public void connectDest(boolean newConn) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {	
		Config config = Config.getInstance();
		config.reloadConfigProperties();

	    String username = config.getConfigProperty("database.dest.username");
	    String password = config.getConfigProperty("database.dest.password");
	    String classJdbc = "oracle.jdbc.driver.OracleDriver";
	    String url = config.getConfigProperty("database.dest.url");
	    if (newConn){
	    	try{
	    		connDest.close();
	    	}catch(Exception e){ }
	    	Class.forName(classJdbc);
	    	connDest=DriverManager.getConnection(url,username,password);
	    	connDest.setAutoCommit(false);
	    }
	    if (connDest == null){
    		Class.forName(classJdbc);
    		connDest=DriverManager.getConnection(url,username,password);
    		connDest.setAutoCommit(false);
	    } else if (connDest.isClosed()) {
			Class.forName(classJdbc);
			connDest=DriverManager.getConnection(url,username,password);
			connDest.setAutoCommit(false);                	    	
	    } 
	}
        
        
    public void connectOrig(boolean newConn) throws FileNotFoundException, IOException, ClassNotFoundException, SQLException {	
    	Config config = Config.getInstance();
		config.reloadConfigProperties();

	    String username = config.getConfigProperty("database.orig.username");
	    String password = config.getConfigProperty("database.orig.password");
	    String classJdbc = "oracle.jdbc.driver.OracleDriver";
	    String url = config.getConfigProperty("database.orig.url");
	    if (newConn){
	    	try{
	    		connOrig.close();
	    	}catch(Exception e){ }
	    	Class.forName(classJdbc);
	    	connOrig=DriverManager.getConnection(url,username,password);
	    	connOrig.setAutoCommit(false);
	    }
	    if (connOrig == null){
    		Class.forName(classJdbc);
    		connOrig=DriverManager.getConnection(url,username,password);
    		connOrig.setAutoCommit(false);
	    } else if (connOrig.isClosed()) {
			Class.forName(classJdbc);
			connOrig=DriverManager.getConnection(url,username,password);
			connOrig.setAutoCommit(false);                	    	
	    } 
	}
                
	public void disconnectOrig() throws Exception{
		if (connOrig != null){
			connOrig.close();
			connOrig=null;
		}
	}

	public void disconnectDest() throws Exception{
		if (connDest != null){
			connDest.close();
			connDest=null;
		}
	}

	public void finalize()throws Exception{
		this.disconnectOrig();
		this.disconnectDest();
	}
	
	
	
	
}
